package com.example.myapplication4;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.distributed.common.UserInfo;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import ohos.rpc.*;
import ohos.utils.net.Uri;
import ohos.utils.zson.ZSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//此Service类用于用户避险调查上传数据的查询操作
public class DemoService4 extends Ability {
    private AbilityContext abilityContext;

    MyRemote myRemote = new MyRemote();
    //日志
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");


    @Override
    protected IRemoteObject onConnect(Intent intent) {

        //返回RemoteObject的对象
        return myRemote.asObject();
    }

    class MyRemote extends RemoteObject implements IRemoteBroker {
        private AbilityContext abilitycontext1;

        public MyRemote(String descriptor, AbilityContext abilitycontext1) {
            super(descriptor);
            this.abilitycontext1 = abilitycontext1;
        }

        public MyRemote() {
            super("descriptor");
        }

        //在此函数中进行js与java的连通
        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {

            switch (code) {
                case 250:
                    //添加返回数据
                    ArrayList<Map<String,Object>> sur=historynum();
                    reply.writeString(ZSONObject.toZSONString(sur));
                    break;

                default:
                    return false;
            }
            return true;
        }

        //查询避险调查的上传历史记录
        private ArrayList<Map<String,Object>> historynum(){
            DataAbilityHelper helper = DataAbilityHelper.creator(DemoService4.this);
            //选定指定数据表
            Uri uri = Uri.parse("dataability:///com.example.myapplication4.UUDataAbility/history");
            //选定指定列
            String[] columns = {"diCat","diNam","diListen","diEscape","diTime"};
            DataAbilityPredicates dataAbilityPredicates = new DataAbilityPredicates();
            ArrayList<Map<String,Object>> all=new ArrayList<>();
            try {
                //返回结果集
                ResultSet rs = helper.query(uri,columns,dataAbilityPredicates);
                int rowCount = rs.getRowCount();
                if (rowCount > 0) {
                    rs.goToNextRow();
                    do {
                        String diCat = rs.getString(0);
                        String diNam=rs.getString(1);
                        int  diListen=rs.getInt(2);
                        String diListenjie="";
                        String diEscapejie="";
                        if(diListen==0){
                            diListenjie="没听到";
                        }else{
                            diListenjie="有听到";
                        }
                        int  diEscape=rs.getInt(3);
                        if(diEscape==0){
                            diEscapejie="未避险";
                        }else{
                            diEscapejie="有避险";
                        }
                        String diTime=rs.getString(4);

                        //添加返回数据
                        Map<String, Object> result = new HashMap<String, Object>();
                        result.put("diCat", diCat);
                        result.put("diNam", diNam);
                        result.put("diListen", diListenjie);
                        result.put("diEscape", diEscapejie);
                        result.put("diTime",diTime);
                        //将result以map方式加入到ArrayList中，便于后续遍历
                        all.add(result);
                    } while (rs.goToNextRow());
                }
            } catch (DataAbilityRemoteException e) {
                e.printStackTrace();
            }
            return all;

        }
        @Override
        public IRemoteObject asObject() {
            return this;
        }

    }
}